const http = require("http")
const client = require("./src/sample")

const app = http.createServer((req, response) => {
    response.writeHead(200, { "Content-Type": "text/plain;charset=utf-8", "Access-Control-Allow-Origin": "*" });
    const params = {
        "Model": "hunyuan-pro",
        "Messages": [
            {
                "Role": "user",
                "Content": "你好"
            }
        ]
    };
    client.ChatCompletions(params).then(
        async (res) => {
          if (typeof res.on === "function") {
            // 流式响应
            res.on("message", (message) => {
              console.log(message.Choices[0].Message)
              response.end("ok - 流式响应")
            })
          } else {
            // 非流式响应
            console.log(res)
            response.end("ok - 非流式响应")
          }
        },
        (err) => {
          console.error("error", err);
          response.end("error")
        }
      );
})

app.listen("3000",() => {
    console.log("server is running at port 3000");
})